package com.vben.system.biz.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vben.common.core.util.R;
import com.vben.system.api.dto.user.*;
import com.vben.system.api.entity.SysUser;
import com.vben.system.api.vo.loginLog.SysUserLoginLogVO;
import com.vben.system.api.vo.user.SysUserVO;
import com.vben.system.biz.service.SysUserRoleService;
import com.vben.system.biz.service.SysUserService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author sunnfun
 */
@RestController
@RequestMapping("/user")
public class SysUserController {

    @Resource
    private SysUserService sysUserService;

    @Resource
    private SysUserRoleService sysUserRoleService;

    /**
     * 分页查询用户
     */
    @GetMapping("/page")
    public R<Page<SysUser>> page(SysUserPageDTO param) {
        return R.ok(sysUserService.pageVO(param));
    }

    /**
     * 获取登陆用户信息
     */
    @GetMapping("/info")
    @SaCheckLogin
    public R<SysUserVO> getLoginUserInfo() {
        return R.ok(sysUserService.getLoginUserInfo());
    }

    /**
     * 根据用户id获取用户信息
     */
    @GetMapping("/detail/{userId}")
    public R<SysUserVO> detail(@PathVariable("userId") Long userId) {
        return R.ok(sysUserService.detail(userId));
    }

    /**
     * 根据用户名查询用户
     */
    @GetMapping("/username")
    public R<SysUser> getUserByUsername(String username) {
        return R.ok(sysUserService.getUserByUsername(username));
    }

    /**
     * 根据手机号查询用户
     */
    @GetMapping("/phone")
    public R<SysUser> getUserByPhone(String phone) {
        return R.ok(sysUserService.getUserByPhone(phone));
    }

    /**
     * 根据用户id查询用户权限列表
     */
    @GetMapping("/permission")
    public R<List<String>> getUserPermissionList(Long userId) {
        return R.ok(sysUserService.getUserPermissionList(userId));
    }

    /**
     * 根据用户id查询角色列表
     */
    @GetMapping("/role")
    public R<List<String>> getUserRoleList(Long userId) {
        return R.ok(sysUserService.getUserRoleList(userId));
    }

    /**
     * 查询当前登陆用户的在线设备列表
     */
    @GetMapping("/onlineDevice")
    @SaCheckLogin
    public R<List<SysUserLoginLogVO>> getOnlineDevice() {
        return R.ok(sysUserService.getOnlineDevice());
    }

    /**
     * 新增用户
     */
    @PostMapping
    public R<Long> addUser(@RequestBody SysUser user) {
        return R.ok(sysUserService.addUser(user));
    }

    /**
     * 更新用户
     */
    @PutMapping
    public R<Boolean> update(@RequestBody SysUser user) {
        return R.ok(sysUserService.updateById(user));
    }

    /**
     * 更新登陆用户信息
     */
    @PutMapping("/login")
    @SaCheckLogin
    public R<Boolean> updateLoginUserInfo(@RequestBody SysUser user) {
        return R.ok(sysUserService.updateLoginUserInfo(user));
    }

    /**
     * 修改登陆用户密码密码
     */
    @PutMapping("/login/password")
    @SaCheckLogin
    public R<Boolean> updatePassword(@RequestBody SysUserUpdatePasswordDTO updatePasswordDTO) {
        return R.ok(sysUserService.updatePassword(updatePasswordDTO));
    }


    @PutMapping("/resetPassword")
    @SaCheckRole("admin")
    public R<Boolean> resetPassword(@RequestBody SysUserResetPasswordDTO resetPasswordDTO) {
        return R.ok(sysUserService.resetPassword(resetPasswordDTO));
    }


    /**
     * 踢用户下线
     */
    @PostMapping("/kickoutByLoginId")
    public R<Boolean> kickoutByLoginId(@RequestBody KickoutDTO dto) {
        return R.ok(sysUserService.kickoutByLoginId(dto));
    }

    /**
     * 锁定用户
     */
    @PostMapping("/locked")
    public R<Boolean> locked(@RequestBody SysUserLockedDTO sysUserLockedDTO) {
        return R.ok(sysUserService.locked(sysUserLockedDTO));
    }

    /**
     * 解锁用户
     */
    @PostMapping("/unlocked")
    public R<Boolean> unlocked(@RequestBody SysUserLockedDTO sysUserLockedDTO) {
        return R.ok(sysUserService.unlocked(sysUserLockedDTO));
    }

    /**
     * 删除用户
     */
    @PostMapping("/delete")
    public R<Boolean> delete(@RequestBody SysUserDeleteDTO sysUserDeleteDTO) {
        return R.ok(sysUserService.delete(sysUserDeleteDTO));
    }

}
